﻿Imports MySql.Data.MySqlClient
Public Class frmproductgroup
    Dim Strsql As String
    Dim Mysql As New ClsSQLhelper
    Public New_Entry_State As Boolean
    Dim DSWHIS As New DSWHIS
    Dim DA As New MySqlDataAdapter
    Dim DT As New DataTable
    Dim BDS As New BindingSource
    Dim commandBuilder As New MySqlCommandBuilder
#Region "Function"
    Sub Load_CategoryData()
        Try



            Strsql = "select category_id,category_name,note,active,sectionid from category where  sectionid = " & Me.Txtid.Text & " order by active desc ,category_name"
            Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
            ' DSWHIS = Mysql.GetMYSQLDataset(Strsql, "DSWHIS", "category")
            DA = New MySqlDataAdapter(Strsql, ClsSQLhelper.MySQLConnectionStr)
            DT = DSWHIS.Tables("category")
            DA.Fill(DT)
            BDS.DataSource = DT


            commandBuilder = New MySqlCommandBuilder(DA)
            ' Pn_Framework.Set_GridSetting(Me.gridcategory, DSWHIS.Tables("category"))
            With gridcategory
                .DataSource = BDS
                .Columns(0).HeaderText = "รหัส"
                .Columns(0).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
                .Columns(0).Width = 60
                .Columns(1).HeaderText = "ชื่อประเภทรายการ"
                .Columns(1).Width = 150
                .Columns(2).HeaderText = "หมายเหตุ"
                .Columns(2).Width = 200
                .Columns(3).HeaderText = "เปิดใช้งาน"
                .Columns(4).Visible = False
                .Columns(4).DefaultCellStyle.DataSourceNullValue = Me.Txtid.Text
            End With
        Catch ex As Exception
            Me.EventLog1.WriteEntry(Me.Name & " : " & ex.Message, EventLogEntryType.Error, 1)
            MsgBox(ex.Message())
        End Try

    End Sub
    Function Validate_Data() As Boolean
        Validate_Data = False

        If Me.gridcategory.RowCount <= 1 Then
            MsgBox("กรุณาระบุประเภทงานอย่างน้อย 1 อย่าง", MsgBoxStyle.Exclamation)
            Exit Function
        End If

        Validate_Data = True
        Return Validate_Data
    End Function
#End Region
    Private Sub BtnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnClose.Click
        Me.Dispose()
    End Sub

    Private Sub BtnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnDelete.Click
        Try
            If MsgBox("คำเตือน การลบ section จะทำให้ category ที่มีอยู่ถูกลบไปด้วย กด yes เพื่อยืนยันกด No เพื่อยกเลิก", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "คุณต้องการลบข้อมูลหรือไม่") = MsgBoxResult.No Then Exit Sub
            Strsql = "Delete from Section where Section_id = " & Me.Txtid.Text
            Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
            Mysql.MySQLExecute(Strsql)

            Strsql = "Delete from category where sectionid = " & Me.Txtid.Text
            Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
            Mysql.MySQLExecute(Strsql)
            'Refresh Data

            ' Load_Data(Me.CboSite.SelectedValue)
        Catch ex As Exception
            Me.EventLog1.WriteEntry(Me.Name & " : " & ex.Message, EventLogEntryType.Error, 1)
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Try
            If Me.TxtName.Text.Trim = "" Then Exit Sub
            If Validate_Data() = False Then Exit Sub
            If MsgBox("คุณต้องการบันทึกข้อมูลหรือไม่", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "ยืนยันการบันทึก") = MsgBoxResult.No Then Exit Sub
            Dim DS As New DataSet
            If New_Entry_State = True Then
                Strsql = "Select Count(*) as count from Section where Section_name= '" & Me.TxtName.Text & "'"
                Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
                If Mysql.MySQLExecuteScalar(Strsql) Then
                    MsgBox("รายการ นี้มีอยู่แล้วไม่สามารถบันทึกซ้ำได้อีก", MsgBoxStyle.Critical)
                    Exit Sub
                End If
                Strsql = "Insert into section (section_name,note) values ('" & Me.TxtName.Text & "', '" & Me.TxtNote.Text & "')"
                Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
                Mysql.MySQLExecute(Strsql)

                Strsql = "Select section_id  from Section where Section_name= '" & Me.TxtName.Text & "'"
                Me.Txtid.Text = Mysql.MySQLExecuteScalar(Strsql)


                DA.Update(CType(BDS.DataSource, DataTable))
                New_Entry_State = False
                MsgBox("เพิ่มข้อมูลเข้าเรียบร้อยแล้ว", MsgBoxStyle.Information)
            Else

                Strsql = "update section set section_name= '" & Me.TxtName.Text & "', note = '" & Me.TxtNote.Text & "' ,active = " & Me.ChkActive.Checked
                Strsql = Strsql & " where section_id = " & Me.Txtid.Text.Trim
                Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
                Mysql.MySQLExecute(Strsql)
           

                DA.Update(CType(BDS.DataSource, DataTable))


            End If

            'Refresh Data
            MsgBox("ปรับปรุงข้อมูลเข้าเรียบร้อยแล้ว", MsgBoxStyle.Information)

        Catch ex As Exception
            Me.EventLog1.WriteEntry(Me.Name & " : " & ex.Message, EventLogEntryType.Error, 1)
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Txtid_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Txtid.TextChanged
        Load_CategoryData()
    End Sub

    Private Sub gridcategory_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles gridcategory.CellContentClick

    End Sub

    Private Sub gridcategory_DefaultValuesNeeded(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowEventArgs) Handles gridcategory.DefaultValuesNeeded
        With Me.gridcategory
            .Rows(e.Row.Index).Cells(4).Value = Me.Txtid.Text
            .Rows(e.Row.Index).Cells(3).Value = True
        End With

    End Sub

    Private Sub Panel1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint

    End Sub
End Class